package com.tarena.lbs.user.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.user.dao.mapper.UserGroupMapper;
import com.tarena.lbs.user.dao.repository.UserGroupRepository;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserGroupPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.query.UserGroupQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserGroupRepositoryImpl implements UserGroupRepository {

    private static final Logger log = LoggerFactory.getLogger(UserGroupRepositoryImpl.class);
    @Autowired
    private UserGroupMapper userGroupMapper;

    @Override
    public PageInfo<UserGroupPO> pageList(UserGroupQuery query) {

        QueryWrapper<UserGroupPO> queryWrapper = assembleUserGroupQuery(query);

        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<UserGroupPO> pos = userGroupMapper.selectList(queryWrapper);
        return new PageInfo<>(pos);
    }
    @Override
    public void save(UserGroupPO po) {
        userGroupMapper.insert(po);
    }
    @Override
    public UserGroupPO findById(Integer id) {
        log.debug("查询ID为{}的人群数据", id);
        return userGroupMapper.selectById(id);
    }

    @Override
    public List<UserGroupPO> userActivatyList(UserGroupQuery query) {
        QueryWrapper<UserGroupPO> queryWrapper = new QueryWrapper<>();
        log.debug("查询参数：{}", query);
        List<Integer> idsList = query.getIds();
        if(query.getIds()!=null && idsList.size()>0){
            queryWrapper.in("id",idsList);
        }
        List<UserGroupPO> poList = userGroupMapper.selectList(queryWrapper);
        return poList;
    }
    @Override
    public void update(UserGroupPO po) {
        // 使用MyBatis-Plus的updateById方法进行更新
        userGroupMapper.updateById(po);
    }
    @Override
    public void delete(Integer id) {
        // 使用MyBatis-Plus的deleteById方法进行删除
        userGroupMapper.deleteById(id);
    }



    private QueryWrapper<UserGroupPO> assembleUserGroupQuery(UserGroupQuery query) {
        QueryWrapper<UserGroupPO> queryWrapper = new QueryWrapper<>();

        //人群名
        if (StringUtils.isNotBlank(query.getGroupName())) {
            //模糊查询
            queryWrapper.like("group_name", query.getGroupName());
        }
        //商家id
        if (query.getBusinessId() != null) {
            queryWrapper.like("business_id", query.getBusinessId());
        }
        //发布状态
        if (query.getStatus() != null) {
            queryWrapper.eq("status", query.getStatus());
        }
        //创建时间
        if (query.getEndTime() != null) {
            queryWrapper.ge("create_at", query.getStartingTime())
                    .le("create_at", query.getEndTime());
        }

        queryWrapper.orderByDesc("create_at");
        queryWrapper.orderByDesc("id");

        return queryWrapper;


    }


}
